<?php
/**
 * Created by PhpStorm.
 * User: Powerless
 * Date: 2017-7-10
 * Time: 13:51
 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\Validate;
use qcos\Cos;


class ShoutController extends AdminBaseController
{
    public function index()
    {
        $param    = $this->request->param();
        $s_name = isset($param['s_name']) ? $param['s_name'] : '';
        $s_status = isset($param['s_status']) ? $param['s_status'] : 0;
        $where = null;
        if($s_name){ $where['s_name'] = ['like', "%$s_name%"];}else{ unset($param['s_name']);}
        if($s_status){ $where['s_status'] = $s_status;}else{ unset($param['s_status']);}
        $shout = Db::name('shout_list')->where($where)->order(["s_status" => "ASC","id" => "DESC"])->paginate(20);
        $shout->appends($param);
        $page = $shout->render();
        $this->assign("s_name", $s_name);
        $this->assign("s_status", $s_status);
        $this->assign("shout", $shout);
        $this->assign("page", $page);
        return $this->fetch();
    }

    public function add()
    {
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                's_name' => 'require|chsAlpha|max:8|unique:shout_list,s_name',
                's_virtual' => 'require|number|between:0,1000000',
                'path' => 'require',
                'file' => 'require',
                's_status' => 'number',
            ]);
            $validate->message([
                's_name.require' => '姓名不可为空',
                's_name.unique'  => '姓名已存在',
                's_name.chsAlpha' => '姓名只能是汉字或字母',
                's_name.max' => '最大姓名长度为8个汉字或字母',
                's_virtual.require' => '预加呼唤数不可为空',
                's_virtual.number' => '预加呼唤数只能是数字',
                's_virtual.between' => '预加呼唤数只能在0~1000000之间',
                'path.require' => '明星的头像不可为空',
                'file.require' => '明星的头像不可为空',
                's_status.number' => '请选择状态',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $data['s_image'] = $this->moveImg($data['path'],$data['file'],4);
                unset($data['path']);
                unset($data['file']);
                Db::name('shout_list')->insert($data);
                $this->success('添加成功',url('shout/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }
    }

    public function edit()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('shout_list')->where(["id" => $id])->find();
        $this->assign("shout", $shout);
        $this->assign("simgs", HTTP_IMG.VERSION_LIVESTAR.'/Default/');
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                's_name' => 'require|chsAlpha|max:8',
                's_virtual' => 'require|number|between:0,1000000',
                's_status' => 'number',
            ]);
            $validate->message([
                's_name.require' => '姓名不可为空',
                's_name.chsAlpha' => '姓名只能是汉字或字母',
                's_name.max' => '最大姓名长度为8个汉字或字母',
                's_virtual.require' => '预加呼唤数不可为空',
                's_virtual.number' => '预加呼唤数只能是数字',
                's_virtual.between' => '预加呼唤数只能在0~1000000之间',
                's_status.number' => '请选择状态',
            ]);
            $data = $this->request->post();
            if ($validate->check($data)) {
                $path = $data['path'];
                $file = $data['file'];
                unset($data['path']);
                unset($data['file']);
                if($path && $file){
                    $imgUrl = parse_url($data['s_image']);
                    $hostUrl = parse_url(HTTP_IMG);
                    if(!empty($imgUrl['host']) && !empty($hostUrl['host']) && ($imgUrl['host'] == $hostUrl['host'])){
                        $this->deleteImg($imgUrl['path']);
                    }
                    $data['s_image'] = $this->moveImg($path,$file,4);
                }
                Db::name('shout_list')->update($data);
                $this->success('修改成功',url('shout/index'));
            }else{
                $this->error($validate->getError());
            }
        }else{
            $this->error('请求错误');
        }

    }

    public function enabled()
    {
        $id    = $this->request->param('id', 0, 'intval');
        $shout = DB::name('shout_list')->where(["id" => $id])->find();
        switch ($shout['s_status']){
            case 10:
                DB::name('shout_list')->where(["id" => $id])->setField('s_status', 20);
                $this->success('您已停用此明星');
                break;
            case 20:
                DB::name('shout_list')->where(["id" => $id])->setField('s_status', 10);
                $this->success('您已启用此明星');
                break;
            default:
                DB::name('shout_list')->where(["id" => $id])->setField('s_status', 20);
                $this->error('未知的明星状态,已置为停用');
        }
    }

    public function uploadImg()
    {
        $file = $_FILES['file'];
        $root = VERSION_LIVESTAR.'/Default/';
        $pathName['path'] = date('Y/m/');
        $pathName['name'] = microtime (true)*10000;
        $path = $root.$pathName['path'];
        $cos = new Cos();
        $type = pathinfo($file["name"]);
        $info = $cos->upload($file["tmp_name"],$path.$pathName['name'].'.'.$type['extension']);
        if($info){
            $data['code'] = 1;
            $data['msg'] = '上传成功';
            $data['data'] = ['path' => $pathName['path'],'file' => $pathName['name'].'.'.$type['extension']];
        }else{
            $data['code'] = 0;
            $data['msg'] = '上传错误';
            $data['data'] = [];
        }
        return json_encode($data);
    }

}